﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Slaks.DocumentSerialization.Document;

namespace TextParsing.Stemmers
{
    class SchenkersStemmer
    {
        private string GetSchenkerStemmedWord(string oldTerm, Hashtable term2LocalNode)
        {
            string newTerm;
            string newTermWithS = oldTerm + "s";
            string newTermWithES = oldTerm + "es";
            string newTermWithING = oldTerm + "ing";

            string newTermWithoutS = String.Empty;
            if (oldTerm.Length > 1 && oldTerm[oldTerm.Length - 1] == 's') newTermWithoutS = oldTerm.Substring(0, oldTerm.Length - 1);

            string newTermWithoutEAndWithIng = String.Empty;
            if (oldTerm.Length > 1 && oldTerm[oldTerm.Length - 1] == 'e')
            {
                newTermWithoutEAndWithIng = oldTerm.Substring(0, oldTerm.Length - 1);
                newTermWithoutEAndWithIng += "ing";
            }



            Node nodeOld = (Node)term2LocalNode[oldTerm];
            Node nodeWithS = (Node)term2LocalNode[newTermWithS];
            Node nodeWithES = (Node)term2LocalNode[newTermWithES];
            Node nodeWithING = (Node)term2LocalNode[newTermWithING];
            Node nodeWithoutS = (Node)term2LocalNode[newTermWithoutS];
            Node nodeWithoutEAndWithIng = (Node)term2LocalNode[newTermWithoutEAndWithIng];

            long freqOld = 0;
            long freqS = 0;
            long freqES = 0;
            long freqING = 0;
            long freqWithoutS = 0;
            long freqWithoutEAndWithIng = 0;


            if (nodeOld != null) freqOld = nodeOld.TotalTermFrequency;
            if (nodeWithS != null) freqS = nodeWithS.TotalTermFrequency;
            if (nodeWithES != null) freqES = nodeWithES.TotalTermFrequency;
            if (nodeWithING != null) freqING = nodeWithING.TotalTermFrequency;
            if (nodeWithoutS != null) freqWithoutS = nodeWithoutS.TotalTermFrequency;
            if (nodeWithoutEAndWithIng != null) freqWithoutEAndWithIng = nodeWithoutEAndWithIng.TotalTermFrequency;

            if (freqOld >= freqS && freqOld >= freqES && freqOld >= freqING && freqOld >= freqWithoutS && freqOld >= freqWithoutEAndWithIng)
            {
                newTerm = oldTerm;
            }
            else if (freqS >= freqES && freqS >= freqING && freqS >= freqOld && freqS >= freqWithoutS && freqS >= freqWithoutEAndWithIng)
            {
                newTerm = newTermWithS;
            }
            else if (freqES >= freqS && freqES >= freqING && freqES >= freqOld && freqES >= freqWithoutS && freqES >= freqWithoutEAndWithIng)
            {
                newTerm = newTermWithES;
            }
            else if (freqWithoutEAndWithIng >= freqS && freqWithoutEAndWithIng >= freqES && freqWithoutEAndWithIng >= freqING && freqWithoutEAndWithIng >= freqWithoutS)
            {
                newTerm = newTermWithoutEAndWithIng;
            }
            else
            {
                newTerm = newTermWithING;
            }

            return newTerm;
        }
    }
}
